<template>
  <div class="container">
    <div class="choose-department">
        <div class="fixd">
            <search placeholder='请输入上级公司' @toQuery="toQuery"></search>
        </div>
        <div class="list" v-if="list.length !== 0">
            <!-- <i-cell class="cell-panel" v-for="(item, index) in list" :key="index" :title="item.F_FullName" url='/pages/center/subCompanyDetail' is-link></i-cell> -->
            <tree :list="list" @handleClick='handleClickOrg'></tree>
        </div>
        <no-result v-else title="暂无上级公司，可点击添加上级公司！"></no-result>
        <buttomBtn v-if="list.length === 0" title="添加上级公司" @handleClick='handleClick'></buttomBtn>
    </div>
  </div>
</template>

<script>
import search from '@/components/search'
import tree from '@/components/tree'
import buttomBtn from '@/components/Bbutton'
import noResult from '@/components/no-result'
import { getOrganizationList, getOrganizeTree } from 'api/api'
export default {
    data () {
        return {
            list: [],
            keyword: ''
        }
    },

    components: {
        search,
        noResult,
        buttomBtn,
        tree
    },

    methods: {
        bindViewTap () {
            const url = '../logs/index'
            wx.navigateTo({ url })
        },
        handleClick () {
            wx.navigateTo({
                url: '/pages/center/addDepartment'
            })
        },
        _getOrganizationList () {
            wx.showLoading({
                title: '加载中'
            })
            getOrganizationList(this.keyword)
                .then(res => {
                    wx.hideLoading()
                    console.log(res)
                    this.list = res.data
                })
        },
        _getOrganizeTree () {
            wx.showLoading({
                title: '加载中'
            })
            getOrganizeTree('')
                .then(res => {
                    wx.hideLoading()
                    console.log(res)
                    this.list = res.data
                })
        },
        toQuery (val) {
            if (!val) {
                return
            }
            this.keyword = val
            this._getOrganizationList()
        },
        handleClickOrg (item) {
            let temp = JSON.parse(JSON.stringify(item))
            temp.childs = []
            let type = this.$root.$mp.query.type
            console.log(type)
            console.log(this.$store)
            if (type === 'addSub') {
                this.$store.commit('org/SETADDSUBCHOOSEINFO', temp)
            } else if (type === 'editSub') {
                this.$store.commit('org/SETEDITSUBCHOOSEINFO', temp)
            } else if (type === 'premium') {
                this.$store.commit('org/SETPREMIUMCHOOSEINFO', temp)
            }
            wx.navigateBack({
                delta: 1
            })
        }
    },
    async onShow () {
        await this._getOrganizeTree()
    }
}
</script>

<style lang='stylus' scoped>
.choose-department
    width 750rpx
    min-height 100vh
    display flex
    flex-direction column
    justify-content space-between
    .fixed
        position fixed
        top 0
        width 100%
    .cell-panel
        margin-top 15rpx
        clear both
    .list
        min-height 90vh
</style>
